// pages/roomSet/roomSet.js
const app = getApp()
var that;
var util = require('../../utils/util.js');
Page({

  data: {

  },


  onLoad: function (options) {
    that = this;
    // 获取会议室列表
    that.getRoomListFun();
  },

  // 获取会议室列表
  getRoomListFun:function(){
    wx.request({
      url: app.http + 'hysMeetingRoom/meetingRoomList',
      data: {
        companyId: app.globalData.loginUserObj.ydCompany.id,
      },
      method: 'get',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("获取会议室列表suc返回", res)
        if (res.data.code == 200) {
          that.setData({
            roomList:res.data.data
          })
          console.log("data",that.data)
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.getRoomListFun();
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.getRoomListFun();
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("fail返回", err)
      }
    })
  },

  // 跳转会议室详情
  toRoomDetailFun: function (e) {
    let index = e.currentTarget.dataset.index;
    wx.setStorage({
      key: 'roomItemObj',
      data: that.data.roomList[index],
      success: function () {
        wx.navigateTo({
          url: '/pages/roomDetail/roomDetail',
        })
      }
    })
  },

  // 编辑会议室
  toEditRoomFun:function(e){
    let index = e.currentTarget.dataset.index;
    wx.setStorage({
      key: 'roomItemObj',
      data: that.data.roomList[index],
      success:function(){
        wx.navigateTo({
          url: '/pages/roomEdit/roomEdit',
        })
      }
    })
   },

  
  // 关闭会议室
  toCloseRoomFun: function (e) {
    let index = e.currentTarget.dataset.index;
    let closing = 0;
    if (that.data.roomList[index].closing == 0){
      closing = 1;
    }else{
      closing = 0;
    }
    wx.request({
      url: app.http + 'hysMeetingRoom/closeMeetingRoom?meetingRoomId=' + that.data.roomList[index].id + '&closing=' + closing,
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("关闭会议室suc返回", res)
        if (res.data.code == 200) {
          that.getRoomListFun();
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.toCloseRoomFun(e);
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.toCloseRoomFun(e);
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("关闭会议室fail返回", err)
      },
      complete: function () {
        that.setData({
          loadingType: false
        })
      }
    })
  },

  // 删除会议室
  toDelRoomFun: function (e) {
    wx.showModal({
      title: '提示',
      content: '确定要删除该会议室吗？',
      success(resShow) {
        if (resShow.confirm) {
          console.log('用户点击确定')
          let index = e.currentTarget.dataset.index;
          that.delRoomReqFun(index);
        } else if (resShow.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 删除接口
  delRoomReqFun:function(indexNum){
    let index = indexNum;
    wx.request({
      url: app.http + 'hysMeetingRoom/deleteMeetingRoom?meetingRoomId=' + that.data.roomList[index].id,
      data: {},
      method: 'DELETE',
      header: {
        'content-type': 'application/json', // 默认值
        'APP-User-Token': app.globalData.tokenMap.access_token
      },
      success: function (res) {
        console.log("关闭会议室suc返回", res)
        if (res.data.code == 200) {
          wx.showToast({
            title: '已删除',
          })
          that.getRoomListFun();
        }
        else if (res.data.code == 100101) {
          // token无效，获取新的token
          var newToken = util.refreshTokenFun(app.globalData.tokenMap.refresh_token).then(
            function (res) {
              if (res.data.code == 200) {
                that.delRoomReqFun(indexNum);
              }
              else if (res.data.code == 400) {
                var wxLoginFun = util.wxLoginFun().then(
                  function (res2) {
                    if (res2.data.code == 200) {
                      that.delRoomReqFun(indexNum);
                    }
                  }
                )
              }
            }
          ).catch(
            function (err) {
              console.log("获取新的token失败返回", err)
            }
          )
        }
      },
      fail: function (err) {
        console.log("关闭会议室fail返回", err)
      },
      complete: function () {
        that.setData({
          loadingType: false
        })
      }
    })
  },

  // 去创建会议室
  toCreatRoomFun:function(){
    wx.navigateTo({
      url: '/pages/roomCreat/roomCreat',
    })
  },

  
})